<?php

namespace App\Models\Traits;

use App\Models\ActionLog;

trait LogAble
{

    public static function bootLogAble()
    {
        static::created(function($model) {
            $employee_id = auth()->id();
            static::storeAction('created', $employee_id, $model, $model->toArray());
        });

        static::updated(function($model) {
            $employee_id = auth()->id();
            $changes = static::getSyncChanges($model);
            static::storeAction('updated', $employee_id, $model, $changes);
        });

        static::deleted(function($model) {
            $employee_id = auth()->id();
            static::storeAction('deleted', $employee_id, $model, '删除数据');
        });
    }

    public static function getSyncChanges($model)
    {
        $changes = [];

        foreach ($model->getDirty() as $key => $value) {
            if (!in_array($key, static::getIgnoreKeys($model))) {
                $original = $model->getOriginal($key);

                if ($original == $value) continue;

                $changes[$key] = [
                    'old' => $original,
                    'new' => $value,
                ];
            }
        }

        $changes = array_merge($changes, ['ip' => request()->ip()]);

        return $changes ?: NULL;
    }
    public static function getActionContent($action_type, $model){
        $action_type_string = $model_string = '';
        switch($action_type){
            case 'created':
                $action_type_string = '创建';
                break;
            case 'updated':
                $action_type_string = '更新';
                break;
            case 'deleted':
                $action_type_string = '删除';
                break;
            default:
                $action_type_string = '未知操作';
                break;
        }

        switch(strtolower($model)){
            case 'language':
                $model_string = '语言';
                break;
            case 'role':
                $model_string = '角色';
                break;
            case 'course':
                $model_string = '课程';
                break;
            case 'user_role':
                $model_string = '用户角色';
                break;
            case 'region':
                $model_string = '大区';
                break;
            case 'country':
                $model_string = '国家';
                break;
            case 'district':
                $model_string = '地区';
                break;
            case 'employee':
                $model_string = '员工';
                break;
            case 'article':
                $model_string = '文章';
                break;
            case 'category':
                $model_string = '分类';
                break;
            case 'banner':
                $model_string = 'Banner';
                break;
            case 'setting':
                $model_string = '设置';
                break;
            case 'type':
                $model_string = '分类';
                break;
            case 'attachment':
                $model_string = '附件';
                break;
            case 'tag':
                $model_string = '标签';
                break;
            default:
                $model_string = $model;
                break;

        }
        return $action_type_string.$model_string;
    }
    public static function storeAction($action_type, $employee_id, $model, $changes = [])
    {
        ActionLog::create([
            'action_id'     => isset($model->id) ? $model->id : $model->user_id,
            'action_type'   => $action_type,
            'action_model'  => str_replace_first('App\\Models\\', '', get_class($model)),
            'action'        => static::getActionContent($action_type, str_replace_first('App\\Models\\', '', get_class($model))),
            'changes'       => json_encode($changes),
            'operator_id'   => empty($employee_id) ? 1 : $employee_id,
            'ip'            => request()->ip(),
        ]);
    }


    public static function getIgnoreKeys($model)
    {
        $modelIgnoreKeys = $model->changeIgnoreKeys?:[];

        return array_merge($modelIgnoreKeys, ['updated_at','created_at','deleted_at']);
    }
}
